package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcDeductDAO;
import com.trkj.dao.jpa.CcDeductdetailDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.pojos.CcDeduct;
import com.trkj.pojos.CcDeductdetail;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcPrepayment;
import com.trkj.vo.CcDeductdetailVo;

@Service
@Transactional
public class CcDeductdetailService {

	@Autowired
	private CcDeductdetailDAO cddDAO;
	@Autowired
	private CcDeductDAO cdDAO;
	@Autowired
	private CcPrepaymentDAO ccppDAO;
	@Autowired
	private CcPatientDAO ccpDAO;
	public Map<String,Object> queryByDeductid(int page,int rows,BigDecimal deductid){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcDeductdetail> list=cddDAO.findByDeductid(pr, deductid);
		
		Map<String,Object> map=new HashMap<>();
		map.put("total", cddDAO.findByDeductidCount(deductid));
		map.put("rows", ccDeductdetailTOCcDeductdetailVo(list));
		return map;
	}
	public List<CcDeductdetailVo> ccDeductdetailTOCcDeductdetailVo(List<CcDeductdetail> list){
		List<CcDeductdetailVo> listVo=new ArrayList<>();
		for(CcDeductdetail cd:list){
			CcDeductdetailVo cdVo=new CcDeductdetailVo();
			cdVo.setDddate(cd.getDddate());
			cdVo.setDdeductid(cd.getDdeductid());
			cdVo.setDdetailid(cd.getDdetailid());
			cdVo.setDdmoney(cd.getDdmoney());
			cdVo.setDdname(cd.getDdname());
			
			CcDeduct cdd=cdDAO.findOne(cdVo.getDdeductid().longValue());
			CcPrepayment cpp=ccppDAO.findOne(cdd.getDpaymentid().longValue());
			CcPatient cp=ccpDAO.findOne(cpp.getPpatientno().longValue());
			cdVo.setPatientid(cp.getPatientid());
			cdVo.setPname(cp.getPname());
			cdVo.setIcid(cp.getIcid());
			
			listVo.add(cdVo);
			
		}
		return listVo;
	}
}
